import 'dart:math';

import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/html_parser.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/pages/news/main/models/comment.dart';
import 'package:industry_net/pages/news/main/models/replay.dart';
import 'package:industry_net/pages/news/news_detail/index.dart';
import 'package:industry_net/pages/news/news_detail/widgets/header.dart';
import 'package:industry_net/pages/news/news_reply/index.dart';

class NewsCommentCell extends StatelessWidget {
  NewsCommentCell({Key? key, required this.md}) : super(key: key);
  final NewsCommentModel md;
  final controller = Get.find<NewsDetailController>();
  @override
  Widget build(BuildContext context) {
    List<NewsCommentModel> _subComments = [];
    if (md.replys != null) {
      if (md.replys!.length > 2) {
        _subComments = md.replys!.sublist(0, 1);
      } else {
        _subComments = md.replys!;
      }
    }

    return Container(
      color: ColorsUtil.white,
      padding: EdgeInsets.fromLTRB(gap12, gap10, gap12, 0),
      child: Column(
        children: [
          NewsCommentItemView(
            key: GlobalKey(),
            md: md,
            callback: controller.actionReply,
            onPraise: controller.actionCommentPraise,
          ),
          _subComments.isNotEmpty
              ? Padding(
                  padding: EdgeInsets.only(left: 35.r),
                  child: Column(
                    children: _subComments
                        .map(
                          (e) => NewsCommentItemView(
                            key: GlobalKey(),
                            md: e,
                            callback: controller.actionReply,
                            onPraise: controller.actionCommentPraise,
                          ),
                        )
                        .toList(),
                  ),
                )
              : const SizedBox(),
        ],
      ),
    );
  }
}

class NewsCommentItemView extends StatelessWidget {
  NewsCommentItemView(
      {Key? key,
      required this.md,
      required this.callback,
      required this.onPraise,
      this.padding})
      : super(key: key);
  final Function(GlobalKey key, NewsCommentModel md) callback;
  final Function(NewsCommentModel md) onPraise;
  final EdgeInsets? padding;
  final NewsCommentModel md;

  final _animKey = GlobalKey();
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => callback(key as GlobalKey, md),
      child: Container(
        padding: padding ?? EdgeInsets.only(bottom: gap10),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            loadAvater(size: 30.r),
            SizedBox(
              width: 5.r,
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: 20.r,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        RichText(
                          text: TextSpan(
                              text: '上岸必有我',
                              style: pfMediumStyle(
                                  color: ColorsUtil.subTitle, fontSize: font13),
                              children: md.isAuthor
                                  ? [
                                      WidgetSpan(
                                          child: Container(
                                        margin: EdgeInsets.only(left: 5.r),
                                        padding: EdgeInsets.symmetric(
                                            horizontal: 3.r),
                                        decoration: BoxDecoration(
                                            color: const Color(0xffdff3e8),
                                            borderRadius:
                                                BorderRadius.circular(2.r)),
                                        child: Text(
                                          '小编',
                                          style: pfMediumStyle(
                                              color: ColorsUtil.green,
                                              fontSize: font10),
                                        ),
                                      ))
                                    ]
                                  : null),
                        ),
                        InkWell(
                          onTap: () => onPraise(md),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '35',
                                style: pfMediumStyle(
                                    color: md.isPraised
                                        ? ColorsUtil.green
                                        : ColorsUtil.content,
                                    fontSize: font12),
                              ),
                              SizedBox(
                                width: 5.r,
                              ),
                              // Icon(
                              //   IconUtil.praise,
                              //   color: md.isPraised
                              //       ? ColorsUtil.green
                              //       : ColorsUtil.content,
                              //   size: 14.r,
                              // ),
                              PraiseAnimateWidget(
                                key: _animKey,
                                isPraised: md.isPraised,
                                isAnimating: md.isAnimating,
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 6.r,
                  ),
                  Text(
                    '还有中介咨询机构，也不知道小编哪里抄的文章，还差的一段。',
                    style: pfMediumStyle(
                        color: ColorsUtil.title, fontSize: font16),
                  ),
                  SizedBox(
                    height: 6.r,
                  ),
                  Row(
                    children: [
                      SizedBox(
                        height: 20.r,
                        child: Text(
                          '15分钟前',
                          style: pfMediumStyle(
                              color: ColorsUtil.content, fontSize: font12),
                        ),
                      ),
                      SizedBox(
                        width: 6.r,
                      ),
                      Container(
                        height: 20.r,
                        padding: EdgeInsets.symmetric(horizontal: 5.r),
                        decoration: BoxDecoration(
                            color: const Color(0xfff5f5f5),
                            borderRadius: BorderRadius.circular(10.r)),
                        child: Text(
                          '3回复',
                          style: pfMediumStyle(
                              color: ColorsUtil.blueDeep, fontSize: font12),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
